From: Jeroen van der Heijden Date: Thu, 27 Sep 2018 09:33:29 +0000 (+0200) Subject: Added tests X-Git-Tag: archive/raspbian/2.0.44-1+rpi1~1^2~3^2~8^2~62 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/success//%22http:/www.example.com/cgi/success/?a=commitdiff_plain;h=6f618d6da9bc32c78c79c9a47c2ff65ba390cf5d;p=siridb-server.git Added tests --- diff --git a/include/siri/cfg/cfg.h b/include/siri/cfg/cfg.h index d121283f..4cc44b19 100644 --- a/include/siri/cfg/cfg.h +++ b/include/siri/cfg/cfg.h @@ -35,9 +35,9 @@ struct siri_cfg_s uint8_t ip_support; uint8_t shard_compression; char server_address[SIRI_CFG_MAX_LEN_ADDRESS]; - char default_db_path[SIRI_PATH_MAX]; + char default_db_path[XPATH_MAX]; uint8_t pipe_support; - char pipe_client_name[SIRI_PATH_MAX]; + char pipe_client_name[XPATH_MAX]; }; #endif /* SIRI_CFG_H_ */ diff --git a/include/strextra/strextra.h b/include/strextra/strextra.h index 5334afab..617155df 100644 --- a/include/strextra/strextra.h +++ b/include/strextra/strextra.h @@ -14,6 +14,7 @@ #include #include +#include void strx_upper_case(char * sptr); void strx_lower_case(char * sptr); diff --git a/include/xpath/xpath.h b/include/xpath/xpath.h index c6fe513c..c2141fd8 100644 --- a/include/xpath/xpath.h +++ b/include/xpath/xpath.h @@ -16,9 +16,9 @@ #include #ifndef PATH_MAX -#define SIRI_PATH_MAX 4096 +#define XPATH_MAX 4096 #else -#define SIRI_PATH_MAX PATH_MAX +#define XPATH_MAX PATH_MAX #endif int xpath_file_exist(const char * fn); diff --git a/src/procinfo/procinfo.c b/src/procinfo/procinfo.c index 6eb0bdd9..4598332c 100644 --- a/src/procinfo/procinfo.c +++ b/src/procinfo/procinfo.c @@ -161,8 +161,8 @@ long int procinfo_open_files(const char * path) DIR * dirp; struct dirent * entry; size_t len = strlen(path); - char buffer[SIRI_PATH_MAX]; - char buf[SIRI_PATH_MAX]; + char buffer[XPATH_MAX]; + char buf[XPATH_MAX]; if ((dirp = opendir("/proc/self/fd")) == NULL) { @@ -173,7 +173,7 @@ long int procinfo_open_files(const char * path) { if (entry->d_type == DT_REG || entry->d_type == DT_LNK) { - snprintf(buffer, SIRI_PATH_MAX, "/proc/self/fd/%s", entry->d_name); + snprintf(buffer, XPATH_MAX, "/proc/self/fd/%s", entry->d_name); if (realpath(buffer, buf) == NULL) { diff --git a/src/siri/cfg/cfg.c b/src/siri/cfg/cfg.c index 0f0ea70e..be7a5dff 100644 --- a/src/siri/cfg/cfg.c +++ b/src/siri/cfg/cfg.c @@ -393,13 +393,13 @@ static void SIRI_CFG_read_pipe_client_name(cfgparser_t * cfgparser) else { len = strlen(option->val->string); - if (len > SIRI_PATH_MAX-2) + if (len > XPATH_MAX-2) { log_warning( "Pipe client name exceeds %d characters, please " "check your configuration file: %s. " "Using default value: '%s'", - SIRI_PATH_MAX-2, + XPATH_MAX-2, siri.args->config, siri_cfg.pipe_client_name); } @@ -451,9 +451,9 @@ static void SIRI_CFG_read_default_db_path(cfgparser_t * cfgparser) } else { - memset(siri_cfg.default_db_path, 0, SIRI_PATH_MAX); + memset(siri_cfg.default_db_path, 0, XPATH_MAX); - if (strlen(option->val->string) >= SIRI_PATH_MAX -2 || + if (strlen(option->val->string) >= XPATH_MAX -2 || realpath( option->val->string, siri_cfg.default_db_path) == NULL) @@ -467,17 +467,17 @@ static void SIRI_CFG_read_default_db_path(cfgparser_t * cfgparser) /* keep space left for a trailing slash and a terminator char */ strncpy(siri_cfg.default_db_path, option->val->string, - SIRI_PATH_MAX - 2); + XPATH_MAX - 2); } len = strlen(siri_cfg.default_db_path); - if (len == SIRI_PATH_MAX - 2) + if (len == XPATH_MAX - 2) { log_warning( "Default database path exceeds %d characters, please " "check your configuration file: %s", - SIRI_PATH_MAX - 3, + XPATH_MAX - 3, siri.args->config); } diff --git a/src/siri/db/db.c b/src/siri/db/db.c index b8dd764b..1d962cba 100644 --- a/src/siri/db/db.c +++ b/src/siri/db/db.c @@ -91,9 +91,9 @@ int8_t siridb_get_idle_percentage(siridb_t * siridb) */ int siridb_is_db_path(const char * dbpath) { - char buffer[SIRI_PATH_MAX]; + char buffer[XPATH_MAX]; snprintf(buffer, - SIRI_PATH_MAX, + XPATH_MAX, "%sdatabase.conf", dbpath); if (!xpath_file_exist(buffer)) @@ -101,7 +101,7 @@ int siridb_is_db_path(const char * dbpath) return 0; /* false */ } snprintf(buffer, - SIRI_PATH_MAX, + XPATH_MAX, "%sdatabase.dat", dbpath); if (!xpath_file_exist(buffer)) @@ -121,7 +121,7 @@ siridb_t * siridb_new(const char * dbpath, int lock_flags) { size_t len = strlen(dbpath); lock_t lock_rc; - char buffer[SIRI_PATH_MAX]; + char buffer[XPATH_MAX]; cfgparser_t * cfgparser; cfgparser_option_t * option = NULL; qp_unpacker_t * unpacker; @@ -167,7 +167,7 @@ siridb_t * siridb_new(const char * dbpath, int lock_flags) /* read database.conf */ snprintf(buffer, - SIRI_PATH_MAX, + XPATH_MAX, "%sdatabase.conf", dbpath); @@ -186,7 +186,7 @@ siridb_t * siridb_new(const char * dbpath, int lock_flags) } snprintf(buffer, - SIRI_PATH_MAX, + XPATH_MAX, "%sdatabase.dat", dbpath); @@ -635,9 +635,9 @@ int siridb_open_files(siridb_t * siridb) */ int siridb_save(siridb_t * siridb) { - char buffer[SIRI_PATH_MAX]; + char buffer[XPATH_MAX]; snprintf(buffer, - SIRI_PATH_MAX, + XPATH_MAX, "%sdatabase.dat", siridb->dbpath); diff --git a/src/siri/db/shards.c b/src/siri/db/shards.c index 84037233..365800ab 100644 --- a/src/siri/db/shards.c +++ b/src/siri/db/shards.c @@ -46,7 +46,7 @@ int siridb_shards_load(siridb_t * siridb) { struct stat st; struct dirent ** shard_list; - char buffer[SIRI_PATH_MAX]; + char buffer[XPATH_MAX]; int n, total, rc = 0; memset(&st, 0, sizeof(struct stat)); @@ -55,7 +55,7 @@ int siridb_shards_load(siridb_t * siridb) siridb_misc_get_fn(path, siridb->dbpath, SIRIDB_SHARDS_PATH); - if (strlen(path) >= SIRI_PATH_MAX - SIRIDB_MAX_SHARD_FN_LEN - 1) + if (strlen(path) >= XPATH_MAX - SIRIDB_MAX_SHARD_FN_LEN - 1) { log_error("Shard path too long: '%s'", path); return -1; @@ -86,7 +86,7 @@ int siridb_shards_load(siridb_t * siridb) { if (is_temp_fn(shard_list[n]->d_name)) { - snprintf(buffer, SIRI_PATH_MAX, "%s%s", + snprintf(buffer, XPATH_MAX, "%s%s", path, shard_list[n]->d_name); log_warning("Removing temporary file: '%s'", buffer); diff --git a/src/siri/help/help.c b/src/siri/help/help.c index d4f87c36..068fb35a 100644 --- a/src/siri/help/help.c +++ b/src/siri/help/help.c @@ -37,8 +37,8 @@ const char * siri_help_get( * path must be initialized for xpath_get_exec_path to handle this variable * correctly. */ - char path[SIRI_PATH_MAX]; - char fn[SIRI_PATH_MAX]; + char path[XPATH_MAX]; + char fn[XPATH_MAX]; memset(&path, 0, sizeof(path)); @@ -52,14 +52,14 @@ const char * siri_help_get( { snprintf( fn, - SIRI_PATH_MAX, + XPATH_MAX, "%shelp/help%s.md", path, help_name + 1); } else { - snprintf(fn, SIRI_PATH_MAX, "%shelp/%s.md", path, help_name); + snprintf(fn, XPATH_MAX, "%shelp/%s.md", path, help_name); } log_debug("Reading help file: '%s'", fn); diff --git a/src/siri/net/pipe.c b/src/siri/net/pipe.c index 09652094..acf8b0f0 100644 --- a/src/siri/net/pipe.c +++ b/src/siri/net/pipe.c @@ -6,7 +6,7 @@ #include #include -#define PIPE_NAME_BUF_SZ SIRI_PATH_MAX +#define PIPE_NAME_BUF_SZ XPATH_MAX /* * Return a name for the connection if successful or NULL in case of a failure. diff --git a/src/xmath/xmath.c b/src/xmath/xmath.c index d7e3659d..328c72a0 100644 --- a/src/xmath/xmath.c +++ b/src/xmath/xmath.c @@ -23,7 +23,9 @@ uint32_t xmath_ipow(int base, int exp) while (exp) { if (exp & 1) + { result *= base; + } exp >>= 1; base *= base; } diff --git a/src/xpath/xpath.c b/src/xpath/xpath.c index 0f7c8b3a..04c4e237 100644 --- a/src/xpath/xpath.c +++ b/src/xpath/xpath.c @@ -84,7 +84,7 @@ ssize_t xpath_get_content(char ** buffer, const char * fn) /* * Get the current executable path. - * (path should at least have size SIRI_PATH_MAX) + * (path should at least have size XPATH_MAX) * * Returns 0 if successful or -1 in case of an error. * (this functions writes logging in case of errors) @@ -93,7 +93,7 @@ int xpath_get_exec_path(char * path) { char* path_end; - if (readlink("/proc/self/exe", path, SIRI_PATH_MAX) == -1) + if (readlink("/proc/self/exe", path, XPATH_MAX) == -1) { log_critical("Cannot read executable path"); return -1; diff --git a/test/test.h b/test/test.h index 2ee19354..82b95332 100644 --- a/test/test.h +++ b/test/test.h @@ -15,6 +15,7 @@ static struct timeval start; static struct timeval end; static int status = TEST_OK; +static int count = 0; const char * padding = ".............................." @@ -22,6 +23,7 @@ const char * padding = static void test_start(char * test_name) { + count = 0; int padlen = 60 - strlen(test_name); printf("Testing %s%*.*s", test_name, padlen, padlen, padding); gettimeofday(&start, 0); @@ -40,6 +42,6 @@ static int test_end(void) return status; } -#define _assert(e) (void)((e)?0:(status = TEST_FAILED) && printf("\n\x1B[33mAssertion failed (%s:%d):\x1B[0m %s\n\n", __FILE__, __LINE__, #e)) +#define _assert(e) (void)((e)?count++:(status = TEST_FAILED) && printf("\n\x1B[33mAssertion failed (%s:%d):\x1B[0m %s\n\n", __FILE__, __LINE__, #e)) #endif /* SIRIDB_TEST_H_ */ \ No newline at end of file diff --git a/test/test.sh b/test/test.sh index e6629766..106003fb 100755 --- a/test/test.sh +++ b/test/test.sh @@ -1,5 +1,13 @@ #!/bin/bash RET=0 +echo -n "Test using valgrind for memory errors and leaks: " +if [[ "$NOMEMTEST" -ne "1" ]] && hash valgrind 2>/dev/null; then + NOMEMTEST=0; + echo -e "\x1B[32menabled\x1B[0m"; +else + NOMEMTEST=1; + echo -e "\x1B[33mdisabled\x1B[0m"; +fi run () { C_SRC=$(cat $1/sources) @@ -8,9 +16,8 @@ run () { OUT=$1.out rm "$OUT" 2> /dev/null - gcc -I"../include" -O0 -g3 -Wall -Wextra -Winline -std=gnu99 $SOURCE $C_SRC -o "$OUT" - if [[ "$NOMEMTEST" -ne "1" ]] && hash valgrind 2>/dev/null; then - echo -n "(memtest) " + gcc -I"../include" -O0 -g3 -Wall -Wextra -Winline -std=gnu99 $SOURCE $C_SRC -lm -lpcre2-8 -lcleri -o "$OUT" + if [[ "$NOMEMTEST" -ne "1" ]]; then valgrind --tool=memcheck --error-exitcode=1 --leak-check=full -q ./$OUT else ./$OUT @@ -24,7 +31,8 @@ if [ $# -eq 0 ]; then run "${d%?}" done else - run "$1" + name=`echo $1 | sed 's/\(test_\)\?\(.*\?\)$/\2/g' | sed 's/\(.*\)\/$/\1/g'` + run "test_$name" fi exit $RET \ No newline at end of file diff --git a/test/test_access/sources b/test/test_access/sources new file mode 100644 index 00000000..16d38cd1 --- /dev/null +++ b/test/test_access/sources @@ -0,0 +1 @@ +../src/siri/db/access.c diff --git a/test/test_access/test_access.c b/test/test_access/test_access.c new file mode 100644 index 00000000..bca9e19c --- /dev/null +++ b/test/test_access/test_access.c @@ -0,0 +1,39 @@ +#include "../test.h" +#include + + +int main() +{ + test_start("access"); + + char buffer[SIRIDB_ACCESS_STR_MAX]; + uint32_t access_bit = 0; + + siridb_access_to_str(buffer, access_bit); + _assert (strcmp(buffer, "no access") == 0); + + access_bit |= SIRIDB_ACCESS_SHOW; + siridb_access_to_str(buffer, access_bit); + _assert (strcmp(buffer, "show") == 0); + + access_bit |= SIRIDB_ACCESS_SELECT; + siridb_access_to_str(buffer, access_bit); + _assert (strcmp(buffer, "select and show") == 0); + + access_bit |= SIRIDB_ACCESS_LIST; + siridb_access_to_str(buffer, access_bit); + _assert (strcmp(buffer, "list, select and show") == 0); + + access_bit |= SIRIDB_ACCESS_PROFILE_WRITE; + siridb_access_to_str(buffer, access_bit); + _assert (strcmp(buffer, "write") == 0); + + access_bit &= ~SIRIDB_ACCESS_INSERT; + siridb_access_to_str(buffer, access_bit); + _assert (strcmp(buffer, "read and create") == 0); + + _assert (siridb_access_from_strn("read", 4) == (SIRIDB_ACCESS_PROFILE_READ)); + _assert (siridb_access_from_strn("list", 4) == SIRIDB_ACCESS_LIST); + + return test_end(); +} \ No newline at end of file diff --git a/test/test_aggr/sources b/test/test_aggr/sources new file mode 100644 index 00000000..0e8f9f41 --- /dev/null +++ b/test/test_aggr/sources @@ -0,0 +1,11 @@ +../src/siri/db/aggregate.c +../src/siri/db/points.c +../src/siri/db/variance.c +../src/siri/db/median.c +../src/siri/db/re.c +../src/siri/err.c +../src/qpack/qpack.c +../src/slist/slist.c +../src/cexpr/cexpr.c +../src/strextra/strextra.c +../src/logger/logger.c diff --git a/test/test_aggr/test_aggr.c b/test/test_aggr/test_aggr.c new file mode 100644 index 00000000..f4e54e0f --- /dev/null +++ b/test/test_aggr/test_aggr.c @@ -0,0 +1,389 @@ +#include +#include "../test.h" +#include +#include + + +#define SIRIDB_MAX_SIZE_ERR_MSG 1024 + +static siridb_aggr_t aggr; +static char err_msg[SIRIDB_MAX_SIZE_ERR_MSG]; + + +static siridb_points_t * prepare_points(void) +{ + siridb_points_t * points = siridb_points_new(10, TP_INT); + uint64_t timestamps[10] = {3, 6, 7, 10, 11, 13, 14, 15, 25, 27}; + int64_t values[10] = {1, 3, 0, 2, 4, 8, 3, 5, 6, 3}; + qp_via_t val; + unsigned int i; + + siridb_init_aggregates(); + + for (i = 0; i < 10; i++) + { + val.int64 = values[i]; + siridb_points_add_point(points, ×tamps[i], &val); + } + + return points; +} + +static int test_count(void) +{ + test_start("aggr (count)"); + + siridb_points_t * aggrp, * points = prepare_points(); + + aggr.gid = CLERI_GID_F_COUNT; + aggr.group_by = 6; + aggr.limit = 0; + aggr.offset = 0; + + aggrp = siridb_aggregate_run(points, &aggr, err_msg); + + _assert (aggrp != NULL); + _assert (aggrp->len == 4); + _assert (aggrp->tp == TP_INT); + _assert (aggrp->data->ts == 6 && aggrp->data->val.int64 == 2); + _assert ((aggrp->data + 3)->ts == 30 && + (aggrp->data + 3)->val.int64 == 2); + + siridb_points_free(aggrp); + siridb_points_free(points); + + return test_end(); +} + +static int test_first(void) +{ + test_start("aggr (first)"); + + siridb_points_t * aggrp, * points = prepare_points(); + + aggr.gid = CLERI_GID_F_FIRST; + aggr.group_by = 5; + aggr.limit = 0; + aggr.offset = 0; + + aggrp = siridb_aggregate_run(points, &aggr, err_msg); + + _assert (aggrp != NULL); + _assert (aggrp->len == 5); + _assert (aggrp->tp == TP_INT); + _assert (aggrp->data->ts == 5 && aggrp->data->val.int64 == 1); + _assert ((aggrp->data + 2)->ts == 15 && + (aggrp->data + 2)->val.int64 == 4); + + siridb_points_free(aggrp); + siridb_points_free(points); + + return test_end(); +} + +static int test_last(void) +{ + test_start("aggr (last)"); + + siridb_points_t * aggrp, * points = prepare_points(); + + aggr.gid = CLERI_GID_F_LAST; + aggr.group_by = 5; + aggr.limit = 0; + aggr.offset = 0; + + aggrp = siridb_aggregate_run(points, &aggr, err_msg); + + _assert (aggrp != NULL); + _assert (aggrp->len == 5); + _assert (aggrp->tp == TP_INT); + _assert (aggrp->data->ts == 5 && aggrp->data->val.int64 == 1); + _assert ((aggrp->data + 2)->ts == 15 && + (aggrp->data + 2)->val.int64 == 5); + + siridb_points_free(aggrp); + siridb_points_free(points); + + return test_end(); +} + +static int test_max(void) +{ + test_start("aggr (max)"); + + siridb_points_t * aggrp, * points = prepare_points(); + + aggr.gid = CLERI_GID_F_MAX; + aggr.group_by = 10; + aggr.limit = 0; + aggr.offset = 0; + + aggrp = siridb_aggregate_run(points, &aggr, err_msg); + + _assert (aggrp != NULL); + _assert (aggrp->len == 3); + _assert (aggrp->tp == TP_INT); + _assert (aggrp->data->ts == 10 && aggrp->data->val.int64 == 3); + _assert ((aggrp->data + 2)->ts == 30 && + (aggrp->data + 2)->val.int64 == 6); + + siridb_points_free(aggrp); + siridb_points_free(points); + + return test_end(); +} + + +static int test_mean(void) +{ + test_start("aggr (mean)"); + + siridb_points_t * aggrp, * points = prepare_points(); + + aggr.gid = CLERI_GID_F_MEAN; + aggr.group_by = 4; + aggr.limit = 0; + aggr.offset = 0; + + aggrp = siridb_aggregate_run(points, &aggr, err_msg); + + _assert (aggrp != NULL); + _assert (aggrp->len == 5); + _assert (aggrp->tp == TP_DOUBLE); + _assert (aggrp->data->ts == 4 && aggrp->data->val.real == 1.0); + _assert ((aggrp->data + 4)->ts == 28 && + (aggrp->data + 4)->val.real == 4.5); + + siridb_points_free(aggrp); + siridb_points_free(points); + + return test_end(); +} + +static int test_median(void) +{ + test_start("aggr (median)"); + + siridb_points_t * aggrp, * points = prepare_points(); + + aggr.gid = CLERI_GID_F_MEDIAN; + aggr.group_by = 7; + aggr.limit = 0; + aggr.offset = 0; + + aggrp = siridb_aggregate_run(points, &aggr, err_msg); + + _assert (aggrp != NULL); + _assert (aggrp->len == 4); + _assert (aggrp->tp == TP_DOUBLE); + _assert (aggrp->data->ts == 7 && aggrp->data->val.real == 1.0); + _assert ((aggrp->data + 1)->ts == 14 && + (aggrp->data + 1)->val.real == 3.5); + + siridb_points_free(aggrp); + siridb_points_free(points); + + return test_end(); +} + +static int test_median_high(void) +{ + test_start("aggr (median_high)"); + + siridb_points_t * aggrp, * points = prepare_points(); + + aggr.gid = CLERI_GID_F_MEDIAN_HIGH; + aggr.group_by = 7; + aggr.limit = 0; + aggr.offset = 0; + + aggrp = siridb_aggregate_run(points, &aggr, err_msg); + + _assert (aggrp != NULL); + _assert (aggrp->len == 4); + _assert (aggrp->tp == TP_INT); + _assert (aggrp->data->ts == 7 && aggrp->data->val.int64 == 1); + _assert ((aggrp->data + 1)->ts == 14 && + (aggrp->data + 1)->val.int64 == 4); + + siridb_points_free(aggrp); + siridb_points_free(points); + + return test_end(); +} + +static int test_median_low(void) +{ + test_start("aggr (median_low)"); + + siridb_points_t * aggrp, * points = prepare_points(); + + aggr.gid = CLERI_GID_F_MEDIAN_LOW; + aggr.group_by = 7; + aggr.limit = 0; + aggr.offset = 0; + + aggrp = siridb_aggregate_run(points, &aggr, err_msg); + + _assert (aggrp != NULL); + _assert (aggrp->len == 4); + _assert (aggrp->tp == TP_INT); + _assert (aggrp->data->ts == 7 && aggrp->data->val.int64 == 1); + _assert ((aggrp->data + 1)->ts == 14 && + (aggrp->data + 1)->val.int64 == 3); + + siridb_points_free(aggrp); + siridb_points_free(points); + + return test_end(); +} + +static int test_min(void) +{ + test_start("aggr (min)"); + + siridb_points_t * aggrp, * points = prepare_points(); + + aggr.gid = CLERI_GID_F_MIN; + aggr.group_by = 2; + aggr.limit = 0; + aggr.offset = 0; + + aggrp = siridb_aggregate_run(points, &aggr, err_msg); + + _assert (aggrp != NULL); + _assert (aggrp->len == 9); + _assert (aggrp->tp == TP_INT); + _assert (aggrp->data->ts == 4 && aggrp->data->val.int64 == 1); + _assert ((aggrp->data + 5)->ts == 14 && + (aggrp->data + 5)->val.int64 == 3); + + siridb_points_free(aggrp); + siridb_points_free(points); + + return test_end(); +} + +static int test_pvariance(void) +{ + test_start("aggr (pvariance)"); + + siridb_points_t * aggrp, * points = prepare_points(); + + aggr.gid = CLERI_GID_F_PVARIANCE; + aggr.group_by = 5; + aggr.limit = 0; + aggr.offset = 0; + + aggrp = siridb_aggregate_run(points, &aggr, err_msg); + + _assert (aggrp != NULL); + _assert (aggrp->len == 5); + _assert (aggrp->tp == TP_DOUBLE); + _assert (aggrp->data->ts == 5 && aggrp->data->val.real == 0.0); + _assert ((aggrp->data + 2)->ts == 15 && + (aggrp->data + 2)->val.real == 3.5); + + siridb_points_free(aggrp); + siridb_points_free(points); + + return test_end(); +} + +static int test_stddev(void) +{ + test_start("aggr (stddev)"); + + siridb_points_t * aggrp, * points = prepare_points(); + + aggr.gid = CLERI_GID_F_STDDEV; + aggr.group_by = 6; + aggr.limit = 0; + aggr.offset = 0; + + aggrp = siridb_aggregate_run(points, &aggr, err_msg); + + _assert (aggrp != NULL); + _assert (aggrp->len == 4); + _assert (aggrp->tp == TP_DOUBLE); + _assert (aggrp->data->ts == 6 && aggrp->data->val.real == sqrt(2.0)); + _assert ((aggrp->data + 1)->ts == 12 && + (aggrp->data + 1)->val.real == 2.0); + + siridb_points_free(aggrp); + siridb_points_free(points); + + return test_end(); +} + +static int test_sum(void) +{ + test_start("aggr (sum)"); + + siridb_points_t * aggrp, * points = prepare_points(); + + aggr.gid = CLERI_GID_F_SUM; + aggr.group_by = 5; + aggr.limit = 0; + aggr.offset = 0; + + aggrp = siridb_aggregate_run(points, &aggr, err_msg); + + _assert (aggrp != NULL); + _assert (aggrp->len == 5); + _assert (aggrp->tp == TP_INT); + _assert (aggrp->data->ts == 5 && aggrp->data->val.int64 == 1); + _assert ((aggrp->data + 2)->ts == 15 && + (aggrp->data + 2)->val.int64 == 20); + + siridb_points_free(aggrp); + siridb_points_free(points); + + return test_end(); +} + +static int test_variance(void) +{ + test_start("aggr (variance)"); + + siridb_points_t * aggrp, * points = prepare_points(); + + aggr.gid = CLERI_GID_F_VARIANCE; + aggr.group_by = 6; + aggr.limit = 0; + aggr.offset = 0; + + aggrp = siridb_aggregate_run(points, &aggr, err_msg); + + _assert (aggrp != NULL); + _assert (aggrp->len == 4); + _assert (aggrp->tp == TP_DOUBLE); + _assert (aggrp->data->ts == 6 && aggrp->data->val.real == 2.0); + _assert ((aggrp->data + 1)->ts == 12 && + (aggrp->data + 1)->val.real == 4.0); + + siridb_points_free(aggrp); + siridb_points_free(points); + + return test_end(); +} + +int main() +{ + return ( + test_count() || + test_first() || + test_last() || + test_max() || + test_mean() || + test_median() || + test_median_high() || + test_median_low() || + test_min() || + test_pvariance() || + test_stddev() || + test_sum() || + test_variance() || + 0 + ); +} diff --git a/test/test_ctree/test_ctree.c b/test/test_ctree/test_ctree.c index 39b6024a..27e12e2a 100644 --- a/test/test_ctree/test_ctree.c +++ b/test/test_ctree/test_ctree.c @@ -1,11 +1,3 @@ -/* - * test_smap.c - * - * Created on: Sep 30, 2017 - * Author: Jeroen van der Heijden - */ - - #include "../test.h" #include @@ -35,8 +27,9 @@ int main() /* test adding values */ { + unsigned int i; _assert (ctree->len == 0); - for (unsigned int i = 0; i < num_entries; i++) + for (i = 0; i < num_entries; i++) { _assert (ct_add(ctree, entries[i], entries[i]) == 0); } @@ -49,7 +42,8 @@ int main() /* test adding duplicated values */ { - for (unsigned int i = 0; i < num_entries; i++) + unsigned int i; + for (i = 0; i < num_entries; i++) { _assert (ct_add(ctree, entries[i], entries[i]) == CT_EXISTS); } @@ -62,7 +56,8 @@ int main() /* test get */ { - for (unsigned int i = 0; i < num_entries; i++) + unsigned int i; + for (i = 0; i < num_entries; i++) { _assert (ct_get(ctree, entries[i]) == entries[i]); } @@ -70,7 +65,8 @@ int main() /* test getn */ { - for (unsigned int i = 0; i < num_entries; i++) + unsigned int i; + for (i = 0; i < num_entries; i++) { _assert (ct_getn( ctree, @@ -81,7 +77,8 @@ int main() /* test pop value */ { - for (unsigned int i = 0; i < num_entries; i++) + unsigned int i; + for (i = 0; i < num_entries; i++) { _assert (ct_pop(ctree, entries[i]) == entries[i]); } diff --git a/test/test_expr/sources b/test/test_expr/sources new file mode 100644 index 00000000..6891284f --- /dev/null +++ b/test/test_expr/sources @@ -0,0 +1 @@ +../src/expr/expr.c diff --git a/test/test_expr/test_expr.c b/test/test_expr/test_expr.c new file mode 100644 index 00000000..eca052d7 --- /dev/null +++ b/test/test_expr/test_expr.c @@ -0,0 +1,26 @@ +#include "../test.h" +#include + + +int main() +{ + test_start("expr"); + + int64_t result; + + _assert(expr_parse(&result, "5+37") == 0 && result == 42); + _assert(expr_parse(&result, "2+2*20") == 0 && result == 42); + _assert(expr_parse(&result, "(2+4)*7") == 0 && result == 42); + _assert(expr_parse(&result, "16%10*7") == 0 && result == 42); + _assert(expr_parse(&result, "7*14%56") == 0 && result == 42); + _assert(expr_parse(&result, "21/3*6") == 0 && result == 42); + _assert(expr_parse(&result, "22/3*6") == 0 && result == 42); + + /* division by zero is not a good idea */ + _assert(expr_parse(&result, "42/(2-2)") == EXPR_DIVISION_BY_ZERO); + + /* module by zero is not a good idea either */ + _assert(expr_parse(&result, "42%(2-2)") == EXPR_MODULO_BY_ZERO); + + return test_end(); +} diff --git a/test/test_grammar/sources b/test/test_grammar/sources new file mode 100644 index 00000000..1038a9ac --- /dev/null +++ b/test/test_grammar/sources @@ -0,0 +1 @@ +../src/siri/grammar/grammar.c diff --git a/test/test_grammar/test_grammar.c b/test/test_grammar/test_grammar.c new file mode 100644 index 00000000..e5190df7 --- /dev/null +++ b/test/test_grammar/test_grammar.c @@ -0,0 +1,41 @@ +#include "../test.h" +#include +#include + +#define assert_valid(__g, __q)(_assert(_is_valid(__g, __q))) +#define assert_invalid(__g, __q)(_assert(!_is_valid(__g, __q))) + +static int _is_valid(cleri_grammar_t * grammar, char * query) +{ + cleri_parse_t * pr = cleri_parse(grammar, query); + _assert (pr); + int is_valid = pr->is_valid; + cleri_parse_free(pr); + return is_valid; +} + +int main() +{ + test_start("grammar"); + + cleri_grammar_t * grammar = compile_grammar(); + + assert_invalid(grammar, "select * from"); + assert_invalid(grammar, "list"); + + assert_valid(grammar, ""); + assert_valid(grammar, "now - 1w"); + assert_valid(grammar, "help # with comments"); + assert_valid(grammar, "select * from *"); + assert_valid(grammar, "select * from 'series'"); + assert_valid(grammar, "select * from * after now-1d"); + assert_valid(grammar, "list series"); + assert_valid(grammar, + "select mean(1h + 1m) from \"series-001\", \"series-002\", " + "\"series-003\" between 1360152000 and 1360152000 + 1d merge as " + "\"series\" using mean(1)"); + + cleri_grammar_free(grammar); + + return test_end(); +} diff --git a/test/test_imap/test_imap.c b/test/test_imap/test_imap.c index 24d3f064..00db4c43 100644 --- a/test/test_imap/test_imap.c +++ b/test/test_imap/test_imap.c @@ -1,11 +1,3 @@ -/* - * test_smap.c - * - * Created on: Sep 30, 2017 - * Author: Jeroen van der Heijden - */ - - #include "../test.h" #include diff --git a/test/test_iso8601/test_iso8601.c b/test/test_iso8601/test_iso8601.c index fb7ccf4c..ddd3a724 100644 --- a/test/test_iso8601/test_iso8601.c +++ b/test/test_iso8601/test_iso8601.c @@ -1,11 +1,5 @@ -/* - * test_smap.c - * - * Created on: Sep 30, 2017 - * Author: Jeroen van der Heijden - */ - - +#include +#include #include "../test.h" #include @@ -14,6 +8,11 @@ int main() { test_start("iso8601"); + /* Update local and timezone */ + (void) setlocale(LC_ALL, ""); + putenv("TZ=:UTC"); + tzset(); + iso8601_tz_t amsterdam = iso8601_tz("Europe/Amsterdam"); _assert(amsterdam > 0); @@ -26,7 +25,6 @@ int main() _assert(iso8601_tz("Ams") < 0); /* Test parsing a year with time-zone information */ - printf("%ld", iso8601_parse_date("2013", amsterdam)); _assert(iso8601_parse_date("2013", amsterdam) == 1356994800); /* Customer offset should be preferred over UTC */ diff --git a/test/test_slist/sources b/test/test_slist/sources new file mode 100644 index 00000000..7a345161 --- /dev/null +++ b/test/test_slist/sources @@ -0,0 +1,2 @@ +../src/slist/slist.c +../src/logger/logger.c \ No newline at end of file diff --git a/test/test_slist/test_slist.c b/test/test_slist/test_slist.c new file mode 100644 index 00000000..ef5c1b03 --- /dev/null +++ b/test/test_slist/test_slist.c @@ -0,0 +1,78 @@ +#include "../test.h" +#include + +const unsigned int num_entries = 14; +char * entries[] = { + "Zero", + "First entry", + "Second entry", + "Third entry", + "Fourth entry", + "Fifth entry", + "Sixth entry", + "Seventh entry", + "8", + "9", + "entry 10", + "entry 11", + "entry 12", + "entry-last" +}; + +int main() +{ + test_start("slist"); + + /* slist_append_safe */ + { + slist_t * slist = slist_new(0); + _assert (slist->len == 0); + _assert (slist->size == 0); + + unsigned int i; + for (i = 0; i < num_entries; i++) + { + _assert (slist_append_safe(&slist, entries[i]) == 0); + } + + /* slist_copy */ + { + slist_t * slistcp = slist_copy(slist); + unsigned int i; + for (i = 0; i < num_entries; i++) + { + _assert (slistcp->data[i] == entries[i]); + } + slist_free(slistcp); + } + + _assert (slist->len == num_entries); + slist_free(slist); + } + + /* slist_append */ + { + slist_t * slist = slist_new(num_entries); + _assert (slist->len == 0); + _assert (slist->size == num_entries); + + unsigned int i; + for (i = 0; i < num_entries; i++) + { + slist_append(slist, entries[i]); + } + + _assert (slist->len == num_entries); + + /* slist_pop */ + for (i = num_entries; i-- > 0;) + { + _assert (slist_pop(slist) == entries[i]); + } + + slist_free(slist); + } + + + return test_end(); +} diff --git a/test/test_strx/sources b/test/test_strx/sources new file mode 100644 index 00000000..f82c4836 --- /dev/null +++ b/test/test_strx/sources @@ -0,0 +1 @@ +../src/strextra/strextra.c diff --git a/test/test_strx/test_strx.c b/test/test_strx/test_strx.c new file mode 100644 index 00000000..8ddc7e55 --- /dev/null +++ b/test/test_strx/test_strx.c @@ -0,0 +1,21 @@ +#include "../test.h" +#include + + +int main() +{ + test_start("strx"); + + /* strx_to_double */ + { + _assert (strx_to_double("0.5", 3) == 0.5); + _assert (strx_to_double("0.55", 3) == 0.5); + _assert (strx_to_double("123.456", 7) == 123.456); + _assert (strx_to_double("123", 3) == 123); + _assert (strx_to_double("123.", 4) == 123); + _assert (strx_to_double("123456.", 3) == 123); + _assert (strx_to_double("-0.5", 3) == -0.5); + } + + return test_end(); +} diff --git a/test/test_version/sources b/test/test_version/sources new file mode 100644 index 00000000..280e66ad --- /dev/null +++ b/test/test_version/sources @@ -0,0 +1 @@ +../src/siri/version.c diff --git a/test/test_version/test_version.c b/test/test_version/test_version.c new file mode 100644 index 00000000..85552d29 --- /dev/null +++ b/test/test_version/test_version.c @@ -0,0 +1,23 @@ +#include "../test.h" +#include + + +int main() +{ + test_start("version"); + + _assert (siri_version_cmp("1.0.0", "2.0.0") < 0); + _assert (siri_version_cmp("2.0.0", "1.0.0") > 0); + _assert (siri_version_cmp("2.2.0", "2.32.0") < 0); + _assert (siri_version_cmp("2.32.0", "2.2.0") > 0); + _assert (siri_version_cmp("2.0.5", "2.0.22") < 0); + _assert (siri_version_cmp("2.0.22", "2.0.5") > 0); + _assert (siri_version_cmp("2.0", "2.0.0") < 0); + _assert (siri_version_cmp("2.0.2", "2.0") > 0); + _assert (siri_version_cmp("a", "") > 0); + _assert (siri_version_cmp("", "b") < 0); + _assert (siri_version_cmp("", "") == 0); + _assert (siri_version_cmp("2.0.0", "2.0.0") == 0); + + return test_end(); +} diff --git a/test/test_xmath/sources b/test/test_xmath/sources new file mode 100644 index 00000000..263b9c7a --- /dev/null +++ b/test/test_xmath/sources @@ -0,0 +1 @@ +../src/xmath/xmath.c diff --git a/test/test_xmath/test_xmath.c b/test/test_xmath/test_xmath.c new file mode 100644 index 00000000..4e8e5190 --- /dev/null +++ b/test/test_xmath/test_xmath.c @@ -0,0 +1,26 @@ +#include "../test.h" +#include + + +int main() +{ + test_start("xmath"); + + /* xmath_ipow */ + { + _assert (xmath_ipow(1000, 0) == 1); + _assert (xmath_ipow(1000, 1) == 1000); + _assert (xmath_ipow(1000, 2) == 1000000); + _assert (xmath_ipow(1000, 3) == 1000000000); + _assert (xmath_ipow(2, 8) == 256); + } + + /* xmath_max_size */ + { + _assert (xmath_max_size(3, 10, 20, 30) == 30); + _assert (xmath_max_size(3, 30, 20, 10) == 30); + _assert (xmath_max_size(3, 10, 30, 20) == 30); + } + + return test_end(); +} diff --git a/test/test_xpath/sources b/test/test_xpath/sources new file mode 100644 index 00000000..2bfe07e8 --- /dev/null +++ b/test/test_xpath/sources @@ -0,0 +1,2 @@ +../src/xpath/xpath.c +../src/logger/logger.c \ No newline at end of file diff --git a/test/test_xpath/test_xpath.c b/test/test_xpath/test_xpath.c new file mode 100644 index 00000000..6b094073 --- /dev/null +++ b/test/test_xpath/test_xpath.c @@ -0,0 +1,16 @@ +#include "../test.h" +#include + + +int main() +{ + test_start("xpath"); + + /* xpath_file_exist */ + { + _assert (xpath_file_exist("./test.h")); + _assert (!xpath_file_exist("./test.foo")); + } + + return test_end(); +}